package net.zigin.nhi.could.waste.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.zigin.nhi.could.waste.annotation.LoginWhitePathAnnotation;
import net.zigin.nhi.could.waste.aop.LoginAspect;
import net.zigin.nhi.could.waste.model.SysUser;
import net.zigin.nhi.could.waste.service.SysUpgradeService;
import net.zigin.nhi.could.waste.service.SysUserService;
import net.zigin.nhi.could.waste.util.OSSClientUtil;
import net.zigin.nhi.could.waste.vo.JsonForResult;
import net.zigin.nhi.could.waste.vo.SysUpgradeVo;
import net.zigin.nhi.could.waste.vo.TokenInfoBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * SysDiction控制器类
 * @ClassName: SysDictionController
 * @author mgw1986
 * @date 2021-9-20 22:06:06
 * @version V1.0
 */
@Api(tags = "文件上传管理")
@RestController
@RequestMapping("/file")
public class FileController extends BaseController {

	@Autowired
	private OSSClientUtil ossClientUtil;
	@Autowired
	private SysUpgradeService sysUpgradeService;
	@Autowired
	private SysUserService sysUserService;

	@PostMapping("/uploadFile")
	@ApiOperation("上传文件")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "file", value = "版本id", required = true, dataType = "MultipartFile")
	})
	public JsonForResult uploadFile(@RequestParam("file") MultipartFile file) {
		String path = ossClientUtil.updateHead(file);
		return JsonForResult.builder().data(path).success(true).build();
	}

	@PostMapping("/uploadHead")
	@ApiOperation("上传文件")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "file", value = "版本id", required = true, dataType = "MultipartFile")
	})
	public JsonForResult uploadHead(@RequestParam("file") MultipartFile file) {
		TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
		String path = ossClientUtil.updateHead(file);
		SysUser sysUser = new SysUser();
		sysUser.setId(tokenInfo.getUserId());
		sysUser.setHeadPic(path);
		sysUserService.updateByPrimaryKeySelective(sysUser);
		return JsonForResult.builder().data(path).success(true).build();
	}

	@PostMapping("/uploadFiles")
	@ApiOperation("多上传文件")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "files", value = "版本id", required = true, allowMultiple = true, dataType = "MultipartFile")
	})
	public JsonForResult uploadFile(@RequestParam("files") List<MultipartFile> files) {
		StringBuffer allPath = new StringBuffer();
		files.forEach(file -> {
			String path = ossClientUtil.updateHead(file);
			allPath.append(path).append(",");
		});
		return JsonForResult.builder().data(allPath.toString()).success(true).build();
	}

	@LoginWhitePathAnnotation
	@GetMapping("/checkUpdate")
	@ApiOperation("检查版本更新")
	public JsonForResult<SysUpgradeVo> checkUpdate(){
		SysUpgradeVo sysUpgradeVo = sysUpgradeService.checkUpdate();
		return JsonForResult.<SysUpgradeVo>builder().data(sysUpgradeVo).success(true).build();
	}

}
